/*
 * @(#)OrderController.java    Created on 2017年5月17日  下午4:59:13
 * Copyright (c) 2017 HaiDai. All rights reserved.
 */
package priv.wjh.test.utils;


import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;


/**
 * @author Robin
 */
@Controller
@RequestMapping("api/order")
@Slf4j
public class OrderController {

    /**
     * 生成形式发票图片与对应表格
     */
    @RequestMapping(value = "/test")
    @ResponseBody
    public void generatePicturesAndTablesOrderList(HttpServletRequest request, HttpServletResponse response) {
        ArrayList<ImageDTO> list = new ArrayList<>();
//        list.add(createImageDTO("小八嘎企业", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 190, 197));
//        list.add(createImageDTO("浙江省杭州市西湖区黄龙体育馆11幢201", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 120, 240));
//        list.add(createImageDTO("知章 15673211038", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 650, 197));
//        list.add(createImageDTO("2023年09月14日", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 660, 275));
//        list.add(createImageDTO("银联支付", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 260, 340));
//        list.add(createImageDTO("上海银行", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 680, 340));
//        list.add(createImageDTO("￥50.00", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 725, 522));
//        list.add(createImageDTO("￥50.00", Color.BLACK, new Font("黑体", Font.PLAIN, 14), 725, 602));
        //操作图片:
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] byteArray;
        try {
            WaterMarkUtils.writeImage(outputStream, list);
            byteArray = outputStream.toByteArray();
            String dateFolder = DateUtils.formatDate(new Date(), "yyyyMMdd");

//            String accessFolder = "/attachment/upload/" + dateFolder;
//            String accessPath = PropertiesUtil.getUploadAccessPath() + "/" + siteId + accessFolder;
//            String folder = PropertiesUtil.getUploadStorePath() + "/" + siteId + accessFolder;
//            String fileName = "/" + UUID.randomUUID().toString().replaceAll("-", "") + "形式发票" + DateUtils.formatDate(new Date(), "yyyyMMdd-hh:mm:ss");
//            String fileUrl = folder + fileName;
//            ossUploadHandler.upload(byteArray, fileUrl);
        }
        catch (IOException e) {
            log.error("生成图片失败", e);
            throw new RuntimeException(e);
        }

        // 打包成zip
        InputStream inputStream = new ByteArrayInputStream(byteArray);
        // 指明response的返回对象是文件流
        response.setContentType("application/octet-stream");
        // 设置在下载框默认显示的文件名
        response.setHeader("Content-Disposition", "attachment;filename=形式发票.zip");
        ServletOutputStream responseOutputStream;
        try {
            responseOutputStream = response.getOutputStream();
            WaterMarkUtils.zipFiles(Collections.singletonList(inputStream), responseOutputStream);
        }
        catch (IOException e) {
            log.error("打包成zip失败", e);
            throw new RuntimeException(e);

        }
    }

}
